﻿Imports System.Data.SqlClient

Public Class frmClientEdit

    Public SNo As Integer

    Public Sub FillContactsList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ClientContacts Where ClientID=" & SNo, cnn)
            Dim Reader As SqlDataReader

            Me.GridContactDetails.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.GridContactDetails.Rows.Add(New String() {Reader.Item("Name"), Reader.Item("Position"), Reader.Item("Email"), Reader.Item("Phone")})
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Sub FillProducersList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim ProducersList As ArrayList = GetProducersList()

            Me.CombProducers.Items.Clear()

            For Each InsurComp As String In ProducersList
                Me.CombProducers.Items.Add(InsurComp)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillBranches()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Branches As ArrayList = GetBranches()

            For Each Branch As String In Branches
                Me.CombDirectBusiness.Items.Add("Direct Business - " & Branch)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmClientRegistryEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillBranches()
        FillProducersList()

        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Clients Where SNo=" & SNo, cnn)
            Dim Reader As SqlDataReader
            Dim Producer As String

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.txtClientName.Text = Reader.Item("FullName")
                Producer = Reader.Item("Producer")
                Me.txtTele.Text = Reader.Item("Tele")
                Me.txtAddress.Text = Reader.Item("Address")
            End While
            cnn.Close()

            If Producer.Contains("Direct Business - ") Then
                Me.CombDirectBusiness.SelectedItem = Producer
                Me.RDirectBusiness.Checked = True
                Me.CombProducers.Enabled = False
            Else
                Me.RProducer.Checked = True
                Me.CombProducers.Enabled = True
                Me.CombProducers.SelectedItem = Producer
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.ErrProv.Clear()
            If Me.txtClientName.Text.Trim.Length = 0 Then
                Me.ErrProv.SetError(Me.txtClientName, "Please fill in all mandatory fields")

            ElseIf Me.RDirectBusiness.Checked = True And Me.CombDirectBusiness.SelectedIndex = -1 Then
                Me.ErrProv.SetError(Me.CombDirectBusiness, "Please fill in all mandatory fields")

            ElseIf Me.RProducer.Checked = True And Me.CombProducers.SelectedIndex = -1 Then
                Me.ErrProv.SetError(Me.CombProducers, "Please fill in all mandatory fields")

            Else
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand
                Dim Trans As SqlTransaction

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Connection = cnn
                cmd.Transaction = Trans

                Dim Producer As String
                If Me.RDirectBusiness.Checked = True Then
                    Producer = Me.CombDirectBusiness.SelectedItem

                ElseIf Me.RProducer.Checked = True Then
                    Producer = Me.CombProducers.SelectedItem
                End If

                '1) Update client details
                cmd.CommandText = "Update Clients Set FullName=@FullName,Producer=@Producer,Tele=@Tele,Address=@Address Where SNo=" & SNo
                cmd.Parameters.AddWithValue("@FullName", Me.txtClientName.Text.Trim)
                cmd.Parameters.AddWithValue("@Producer", Producer)
                cmd.Parameters.AddWithValue("@Tele", Me.txtTele.Text.Trim)
                cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text.Trim)
                cmd.ExecuteNonQuery()

                '2) Save contacts details
                cmd.CommandText = "Delete From ClientContacts Where ClientID=" & SNo
                cmd.ExecuteNonQuery()

                cmd.CommandText = "Insert Into ClientContacts (ClientID,Name,Position,Email,Phone,SavedUser) Values " & _
                                  "(@ClientID,@Name,@Position,@Email,@Phone,@SavedUser) "

                For Each row As DataGridViewRow In Me.GridContactDetails.Rows
                    If row.Index <> Me.GridContactDetails.RowCount - 1 Then
                        cmd.Parameters.Clear()
                        cmd.Parameters.AddWithValue("@ClientID", SNo)
                        cmd.Parameters.AddWithValue("@Name", row.Cells(Me.DGColName.Index).Value)
                        cmd.Parameters.AddWithValue("@Position", row.Cells(Me.DGColPosition.Index).Value)
                        cmd.Parameters.AddWithValue("@Email", row.Cells(Me.DGColEmail.Index).Value)
                        cmd.Parameters.AddWithValue("@Phone", row.Cells(Me.DGColTele.Index).Value)
                        cmd.Parameters.AddWithValue("@SavedUser", CurrentUser)
                        cmd.ExecuteNonQuery()
                    End If
                Next

                Trans.Commit()
                cnn.Close()

                Me.Cursor = Cursors.Default

                Me.Close()
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub CheckRadio()
        If Me.RDirectBusiness.Checked = True Then
            Me.CombDirectBusiness.Enabled = True
            Me.CombProducers.SelectedIndex = -1
            Me.CombProducers.Enabled = False

        ElseIf Me.RProducer.Checked = True Then
            Me.CombDirectBusiness.Enabled = False
            Me.CombDirectBusiness.SelectedIndex = -1
            Me.CombProducers.Enabled = True
        End If
    End Sub

    Private Sub RDirectBusiness_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RDirectBusiness.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub RProducer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RProducer.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub GridContactDetails_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridContactDetails.CellClick
        Try
            If e.ColumnIndex = Me.DGColDelete.Index Then
                Me.GridContactDetails.Rows.RemoveAt(e.RowIndex)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub GridContactDetails_CellEndEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridContactDetails.CellEndEdit
        For Each row As DataGridViewRow In Me.GridContactDetails.Rows
            For i As Integer = 0 To Me.GridContactDetails.Columns.Count - 1
                If row.Cells(i).Value Is Nothing Then
                    row.Cells(i).Value = ""
                End If
            Next
        Next
    End Sub
End Class